package com.hua.store.web.filter;

import com.hua.store.domin.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class SourceFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        User user = (User) req.getSession().getAttribute("loginUser");
        if (user != null) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            req.setAttribute("msg", "请登录后再访问");
            req.getRequestDispatcher("/jsp/test.jsp").forward(servletRequest, servletResponse);
        }
    }
}
